package basic.study.wantOffer.chapter3;

/**
 * @ClassName Problem26
 * @Description
 * @Company inspur
 * @Author Kevin
 * @Date 2020/5/15 9:14
 * @Version 1.0
 */
public class Problem26 {
    public boolean HasSubTree(BinaryTreeNode node1, BinaryTreeNode node2) {

        ;boolean result = false;
        if (node1 != null && node2 != null) {
            if (node1.value == node2.value) {
                result = DoesTreeHaveTree2(node1, node2);
            }
            if (!result) {
                result = HasSubTree(node1.left, node2.left);
            }
            if (!result) {
                result = HasSubTree(node1.right, node2.right);
            }
        }
        return result;
    }

    private boolean DoesTreeHaveTree2(BinaryTreeNode node1, BinaryTreeNode node2) {
        if (node2 == null) {
            return true;
        }
        if (node1 == null) {
            return false;
        }
        //如果是double，使用equals，不能使用==
        if (node1.value != node2.value) {
            return false;
        }
        return DoesTreeHaveTree2(node1.left, node2.left) && DoesTreeHaveTree2(node1.right, node2.right);
    }

}
